package models

import (
	"github.com/jinzhu/gorm"
	"os"
	//_ "github.com/jinzhu/gorm/dialects/sqlite"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

var (
	db *gorm.DB
)
func init(){
	//logs.Info("你好  吴江攀")
	var err error
	if err = os.MkdirAll("data",0777); err!=nil{
		panic("failed "+err.Error())
	}
	//db, err = gorm.Open("sqlite3", "data/data.db")
	db, err =gorm.Open("mysql", "root:root@tcp(127.0.0.1:3306)/liteblog?charset=utf8&parseTime=true&loc=Local")
	if err != nil {
		panic("failed to connect database")
	}
	//defer db.Close()
	db.AutoMigrate(&User{},&Note{},&Message{},&PraiseLog{})
	//数据库没有用户数据  新增一个用户数据
	var count int
	if err:= db.Model(&User{}).Count(&count).Error;err==nil&&count==0{
		db.Create(&User{
			Name :"admin",
			Email:"admin@qq.com",
			Pwd :"admin",
			Avartar :"/static/images/info-img.png",
			Role :0,
		})
	}
	// Migrate the schema
	//db.AutoMigrate(&Product{})

	//// 创建
	//db.Create(&Product{Code: "L1212", Price: 1000})
	//
	//// 读取
	//var product Product
	//db.First(&product, 1) // 查询id为1的product
	//db.First(&product, "code = ?", "L1212") // 查询code为l1212的product
	//
	//// 更新 - 更新product的price为2000
	//db.Model(&product).Update("Price", 2000)
	//
	//// 删除 - 删除product
	//db.Delete(&product)
}